//
//  DestinationMoreViewController.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/24.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "DestinationMoreViewController.h"

@interface DestinationMoreViewController ()

@end

@implementation DestinationMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.allArray = [NSMutableArray array];
    [self getDataValue];
    self.navigationItem.title = self.des.title;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置item大小
    flowLayout.itemSize = CGSizeMake((WIDTH - 45) / 2, (WIDTH - 45) / 2);
 
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //距离边界的间距 上 左 下 右
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 14, 15, 14);
    //列间距
    flowLayout.minimumInteritemSpacing = 15;
    //行间距
    flowLayout.minimumLineSpacing = 15;
    
    self.destinationMoreCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113) collectionViewLayout:flowLayout];
    self.destinationMoreCollectionView.delegate = self;
    self.destinationMoreCollectionView.dataSource = self;
    self.destinationMoreCollectionView.backgroundColor = [UIColor whiteColor];
    //注册方法
    [self.destinationMoreCollectionView registerClass:[DestinationMoreCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];

    
    [self.view addSubview:self.destinationMoreCollectionView];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中...";
    [self.HUD show:YES];
    
}
-(void)getDataValue {
    
    NSString *str_url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%@/", self.des.index];

    
    [AFNGetDataValue getDataAFN:str_url block:^(NSMutableDictionary *dataDic) {
        
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:[dataDic objectForKey:@"data"]];
        for (NSMutableDictionary *dic in array) {
            DestinationMore *des = [[DestinationMore alloc] init];
            [des setValuesForKeysWithDictionary:dic];
            des.type = [NSString stringWithFormat:@"%@", [dic objectForKey:@"type"]];
            des.placeId = [NSString stringWithFormat:@"%@", [dic objectForKey:@"id"]];
            [self.allArray addObject:des];
        }
        

        [self.destinationMoreCollectionView reloadData];
        [self.HUD hide:YES];
    }];
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DestinationDetailViewController *destinationD = [[DestinationDetailViewController alloc] init];
    DestinationMore *desM = self.allArray[indexPath.row];
    Destination *des = [[Destination alloc] init];
    des.placeId = desM.placeId;
    des.type = desM.type;
    destinationD.des = des;
    [self.navigationController pushViewController:destinationD animated:YES];
    
}

//设置多个分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}




//设置item的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    

    
    return self.allArray.count;
}

//布局item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    //直接从系统的重用池里取ite
    DestinationMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    if (self.allArray.count != 0) {
        
        
        DestinationMore *des = self.allArray[indexPath.row];
        cell.des = des;
        
    }
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
